<template>
  <PerfectTableComponent
    v-bind="$props"
    v-model:multipleSelection="multipleSelection"
    :loadTableList="loadTableList"
    :changePageSize="changePageSize"
    :changePage="changePage"
    :removeQueryField="removeQueryField"
    :clearQuery="clearQuery"
    v-model:query="query"
    title="物料"
    ref="tableRef"
    :queryFormHeaders="queryFormHeaders"
    :table="table"
    :tableColumns="tableColumns"
    :showTableIndex="false"
    :baseUrl="urls.API_URL_BACKSTAGE_MES_MATERIAL"
    v-model:form="form"
    :formHeader="formHeader"
    :showToolBar="showToolBar"
    @beforeAddRow="beforeAddRow"
    @afterLoadDataSuccessInEditRow="afterLoadDataSuccessInEditRow"
    @beforeFormSubmit="beforeFormSubmit"
  >
    <!-----------------------------表格自定义列----------------------------------------->
    <!--所属栏目1-->
    <template #table_column_materialTypeName="scope: any">
      {{ scope.row.materialType != null ? scope.row.materialType.name : '' }}
    </template>
  </PerfectTableComponent>
</template>

<script setup lang="ts">
import PerfectTableComponent from '@/components/base/table/PerfectTableComponent.vue';
import { onMounted, provide, reactive, ref } from 'vue';
import useTableList from '@/api/hooks/useTableList';
import type { FormHeader, ResponseData, TableColumn } from '@/types/interface/base_interface';
import urls from '@/api/request/urls';
import { getDict } from '@/api/dict';
import type { Material } from '@/types/interface/mes_interface';
import Request from '@/api/request/request';

// 表格工具必备参数
const { table, query, loadTableList, removeQueryField, clearQuery, changePageSize, changePage } = useTableList(urls.API_URL_BACKSTAGE_MES_MATERIAL);
// 将表格刷新函数传到子容器
provide('loadTableList', loadTableList);
// 复选框选择的记录row集合
const multipleSelection = defineModel<any[]>('multipleSelection', {
  required: false,
  default: []
});
// 接收父组件（因为本组件供其他组件调用，因此封装）传递过来的值
interface Props {
  showToolBar?: Boolean; // 是否显示表格工具栏，默认显示
  showOperatorColumn?: Boolean; // 是否显示表格右侧的操作栏，默认显示
  showAddNewButton?: Boolean; // 在表格工具栏是否显示新增按钮
  showDeleteButton?: Boolean; // 是否显示删除按钮，如果不显示则整个表格插件都不显示
  showEditButton?: Boolean; //  是否显示编辑按钮，如果不显示则整个表格插件都不显示
  operatorColumnWidth?: number; // 表格右侧操作了大小
  dialogConfirmButtonText?: string; // 确认按钮上的文字
}
const props = withDefaults(defineProps<Props>(), {
  showToolBar: () => Boolean(true), //默认显示
  operatorColumnWidth: 300
});

//查询==========================================================
const queryFormHeaders = ref<FormHeader[]>([]);

//表格==========================================================
// 表格插件ref对象1
const tableRef = ref();

//表格列
// 定义表格所有的列字段信息
const tableColumns = ref<TableColumn[]>([]);

//表单==========================================================
//表单对象
const form = ref<Material>({});
//表单元素类型
const formHeader = ref<FormHeader[]>([]);

const getMaterialType = async () => {
  const resData: any = await new Request(urls.API_URL_BACKSTAGE_MES_MATERIALTYPE + '/all').get();
  if (resData.success) {
    return resData.data;
  }
};
onMounted(async () => {
  //获取物料类别列表
  const materialTypes = await getMaterialType();
  //定义优先级选项。放在mounted中执行时，要将引用改变量的属性也放进来。
  const enabledDict = await getDict('启用状态', 'boolean');
  //定义物料类别选项。放在mounted中执行时，要将引用改变量的属性也放进来。
  const typeDict = await getDict('物料类型', 'number');
  queryFormHeaders.value = [
    // 查询表单
    {
      prop: 'name',
      label: '物料名称'
    },
    {
      prop: 'materialTypeId',
      propAsName: 'materialType.id',
      label: '物料类别',
      type: 'selectObject',
      list: materialTypes,
      config: {
        valueKey: 'id',
        defaultProp: {
          value: 'id',
          label: 'name'
        }
      }
    },
    {
      label: '物料类型',
      prop: 'type',
      type: 'select',
      list: typeDict,
      rules: [
        {
          required: true,
          message: '不能为空',
          trigger: 'blur'
        }
      ]
    },
    {
      prop: 'enabled',
      label: '是否启用',
      type: 'select',
      list: enabledDict
    },
    {
      prop: 'model',
      label: '物料型号',
      show: false
    },
    {
      prop: 'specification',
      label: '物料规格',
      show: false
    },
    {
      prop: 'unit',
      label: '物料单位',
      show: false
    }
  ];
  //表格对象
  tableColumns.value = [
    {
      label: '名称',
      prop: 'name',
      fixed: true,
      width: 150
    },
    {
      label: '所属类别',
      prop: 'materialTypeName',
      type: 'custom',
      width: 100
    },
    {
      label: '物料型号',
      prop: 'model',
      width: 100
    },
    {
      label: '物料规格',
      prop: 'specification',
      width: 100
    },
    {
      label: '物料单位',
      prop: 'unit',
      width: 100
    },
    {
      label: '物料单价',
      prop: 'price',
      width: 100
    },
    {
      label: '库存',
      prop: 'inventory',
      width: 100
    },
    {
      label: '物料类型',
      prop: 'type',
      width: 100,
      type: 'dict',
      dict: typeDict
    },
    {
      label: '启用状态',
      prop: 'enabled',
      width: 100,
      type: 'dict',
      dict: enabledDict,
      config: {
        dictTag: {
          primary: [true],
          danger: [false]
        }
      }
    },
    {
      label: '使用工序数',
      prop: 'carTechnologyDetailNumber',
      width: 100
    },
    {
      label: '创建时间',
      prop: 'createTime',
      width: 180
    },
    {
      label: '最后编辑时间',
      prop: 'updateTime',
      width: 180
    }
  ];
  // 添加编辑表单
  formHeader.value = [
    {
      prop: 'materialTypeId',
      label: '物料类别',
      type: 'selectObject',
      list: materialTypes,
      config: {
        valueKey: 'id',
        defaultProp: {
          value: 'id',
          label: 'name'
        }
      },
      rules: [
        {
          required: true,
          message: '不能为空',
          trigger: 'blur'
        }
      ]
    },
    {
      label: '名称',
      prop: 'name',
      rules: [
        {
          required: true,
          message: '不能为空',
          trigger: 'blur'
        },
        {
          min: 1,
          max: 60,
          message: '长度在 1 到 60个字符',
          trigger: 'blur'
        }
      ]
    },
    {
      label: '物料型号',
      prop: 'model',
      rules: [
        {
          max: 100,
          message: '长度不能超过100个字符',
          trigger: 'blur'
        }
      ]
    },
    {
      label: '物料规格',
      prop: 'specification',
      rules: [
        {
          max: 100,
          message: '长度不能超过100个字符',
          trigger: 'blur'
        }
      ]
    },
    {
      label: '物料单位',
      prop: 'unit',
      rules: [
        {
          max: 100,
          message: '长度不能超过50个字符',
          trigger: 'blur'
        }
      ]
    },
    {
      label: '物料单价',
      prop: 'price',
      type: 'number',
      config: {
        number: {
          precision: 2,
          min: 0,
          suffix: '元'
        }
      }
    },
    {
      label: '物料类型',
      prop: 'type',
      type: 'select',
      list: typeDict,
      rules: [
        {
          required: true,
          message: '不能为空',
          trigger: 'blur'
        }
      ]
    },
    {
      label: '库存',
      prop: 'inventory',
      type: 'number',
      rules: [
        {
          required: true,
          message: '不能为空',
          trigger: 'blur'
        }
      ]
    },
    {
      label: '是否启用',
      prop: 'enabled',
      type: 'select',
      list: enabledDict,
      rules: [
        {
          required: true,
          message: '不能为空',
          trigger: 'blur'
        }
      ]
    }
    // {
    //   label: '仓储设置',
    //   prop: 'ccsz',
    //   type: 'divider'
    // },
  ];
});
//在添加行的动作以前
const beforeAddRow = async () => {
  //传入初始值
  form.value = {
    inventory: 0,
    enabled: true
  };
};
//在编辑行读取后端记录后需要修改的地方
const afterLoadDataSuccessInEditRow = async (formEdit: Material) => {
  //重新封装表单对象的值
  if (formEdit.materialType != null) {
    formEdit.materialTypeId = formEdit.materialType.id;
  }
};
//在表单提交前对对象进行封装
const beforeFormSubmit = () => {
  if (form.value.materialTypeId != null) {
    form.value.materialType = {
      id: form.value.materialTypeId
    };
  }
};
//清空表格选中的复选框
const clearSelection = () => tableRef.value!.clearSelection();
defineExpose({
  clearSelection
});
</script>

<style scoped></style>
